//
//  WebViewController.m
//  QLadder
//
//  Created by xiongguobing on 2019/7/29.
//  Copyright © 2019 qding. All rights reserved.
//

#import "WebViewController.h"
#import "MyURLProtocol.h"
#import "MyURLCache.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"

@interface WebViewController ()<UITextFieldDelegate, UIWebViewDelegate, NJKWebViewProgressDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (nonatomic, strong) NJKWebViewProgress * progressProxy;
@property (nonatomic, strong) NJKWebViewProgressView * progressView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [NSURLProtocol registerClass:[MyURLProtocol class]];
    MyURLCache * cache = [MyURLCache new];
    [NSURLCache setSharedURLCache:cache];
    
    self.webView.delegate = self.progressProxy;
    [self.navigationController.navigationBar addSubview:self.progressView];

    [self beginLoad];
    // Do any additional setup after loading the view.
}

- (UIBarButtonItem *)backItem
{
    return [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav-back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemClicked)];
}

- (void)backItemClicked
{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }
}

- (IBAction)gotoItemClicked:(id)sender
{
    [self beginLoad];
}

- (void)beginLoad
{
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlTF.text]]];
    NSURL * fileURL = [[NSBundle mainBundle] URLForResource:@"main.html" withExtension:nil];
    NSURLRequest * request = [NSURLRequest requestWithURL:fileURL];
    [self.webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if ([webView canGoBack]) {
        self.navigationItem.leftBarButtonItem = [self backItem];
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [self.progressView setProgress:progress animated:YES];
}

#pragma mark - setter and getter
- (NJKWebViewProgress *)progressProxy
{
    if (!_progressProxy) {
        _progressProxy = [[NJKWebViewProgress alloc] init]; // instance variable
        _progressProxy.webViewProxyDelegate = self;
        _progressProxy.progressDelegate = self;
    }
    return _progressProxy;
}

- (NJKWebViewProgressView *)progressView
{
    if (!_progressView) {
        CGFloat progressBarHeight = 2.f;
        CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
        CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
        _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
        _progressView.progressBarView.backgroundColor = RedColor;
        _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    }
    return _progressView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
